{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# color detection using OpenCV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "#programming_fever\n",
    "import cv2\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "img_path = \"D://OpenCV//shape-detection//New folder//palette.jpg\"\n",
    "img = cv2.imread(img_path)\n",
    "img=cv2.resize(img,(700,500))\n",
    "\n",
    "clicked = False\n",
    "r = g = b = xpos = ypos = 0\n",
    "\n",
    "#Reading csv file with pandas and giving names to each column\n",
    "index=[\"color\",\"color_name\",\"hex\",\"R\",\"G\",\"B\"]\n",
    "csv = pd.read_csv('colors.csv', names=index, header=None)\n",
    "\n",
    "#function to calculate minimum distance from all colors and get the most matching color\n",
    "def getColorName(R,G,B):\n",
    "    minimum = 10000\n",
    "    for i in range(len(csv)):\n",
    "        d = abs(R- int(csv.loc[i,\"R\"])) + abs(G- int(csv.loc[i,\"G\"]))+ abs(B- int(csv.loc[i,\"B\"]))\n",
    "        if(d<=minimum):\n",
    "            minimum = d\n",
    "            cname = csv.loc[i,\"color_name\"]\n",
    "    return cname\n",
    "\n",
    "#function to get x,y coordinates of mouse double click\n",
    "def draw_function(event, x,y,flags,param):\n",
    "    if event == cv2.EVENT_LBUTTONDBLCLK:\n",
    "        global b,g,r,xpos,ypos, clicked\n",
    "        clicked = True\n",
    "        xpos = x\n",
    "        ypos = y\n",
    "        b,g,r = img[y,x]\n",
    "        b = int(b)\n",
    "        g = int(g)\n",
    "        r = int(r)\n",
    "cv2.namedWindow('color detection by programming_fever')\n",
    "cv2.setMouseCallback('color detection by programming_fever',draw_function)\n",
    "\n",
    "while(1):\n",
    "\n",
    "    cv2.imshow(\"color detection by programming_fever\",img)\n",
    "    if (clicked):\n",
    "   \n",
    "        #cv2.rectangle(image, startpoint, endpoint, color, thickness)-1 fills entire rectangle \n",
    "        cv2.rectangle(img,(20,20), (750,60), (b,g,r), -1)\n",
    "\n",
    "        #Creating text string to display( Color name and RGB values )\n",
    "        text = getColorName(r,g,b) + ' R='+ str(r) +  ' G='+ str(g) +  ' B='+ str(b)\n",
    "        \n",
    "        #cv2.putText(img,text,start,font(0-7),fontScale,color,thickness,lineType )\n",
    "        cv2.putText(img, text,(50,50),2,0.8,(255,255,255),2,cv2.LINE_AA)\n",
    "\n",
    "        #For very light colours we will display text in black colour\n",
    "        if(r+g+b>=600):\n",
    "            cv2.putText(img, text,(50,50),2,0.8,(0,0,0),2,cv2.LINE_AA)\n",
    "            \n",
    "        clicked=False\n",
    "\n",
    "    if cv2.waitKey(20) & 0xFF ==27:\n",
    "        break\n",
    "    \n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
